$('#startfly').on('click', startFly)
$('#stopfly').on('click', stopFly)

function startFly(){
    //界面控制
    $('#startfly').addClass('hide');
    $('#firestart').addClass('hide');
    $('#escape').addClass('hide');
    $('#floorclose').addClass('hide');
    
    //outdoorTest change
    viewer.entities.removeAll();
    viewer.scene.scanEffect.show = false;
    if(viewer.scene.primitives._primitives[1]){
        viewer.scene.primitives.remove(viewer.scene.primitives._primitives[1])
    };
    viewer.scene.camera.flyTo({
        destination: { x: -2386096.230671791, y: 5386274.57538463, z: 2444589.4807831007 },
        orientation:{
            heading: 0,
            pitch:0,
            roll:0
        },
        duration: 3
    });
    //动画3s

    //界面控制
    var startFlyManual = setTimeout(function(){
        $('#stopfly').removeClass('hide');
        $('#floorclose').removeClass('hide');
    },3000)

    viewer.scene.screenSpaceCameraController.enableRotate = false;
    viewer.scene.screenSpaceCameraController.enableTranslate = false;
    viewer.scene.screenSpaceCameraController.enableZoom = false;
    viewer.scene.screenSpaceCameraController.enableTilt = false;
    viewer.scene.screenSpaceCameraController.enableLook = false;
    
    var camera = viewer.camera;
    var ellipsoid = viewer.scene.globe.ellipsoid;
    var cameraHeight = ellipsoid.cartesianToCartographic(camera.position).height
    var moveRate = cameraHeight / 1800.0;
    var flags = {
        looking: false,
        moveForward: false,
        moveBackward: false,
        moveUp: false,
        moveDown: false,
        moveLeft: false,
        moveRight: false,
        turnLeft: false,
        turnRight: false,
        turnHigh: false,
        turnlow: false,
    };

    function getFlagForKeyCode(keyCode) {
        switch(keyCode){
            case 'W'.charCodeAt(0):
                return 'moveForward';
            case 'S'.charCodeAt(0):
                return 'moveBackward';
            case 38:
                return 'moveUp';
            case 40:
                return 'moveDown';
            case 39:
                return 'moveRight';
            case 37:
                return 'moveLeft';
            case 81:
                return 'lookUp';
            // return 'turnLeft';//Q
            case 69:
                return 'lookDown';
            // return 'turnRight';//E
            case 65:
                return 'turnLeft'
            // return 'turnHigh';//A
            case 68:
                return 'turnRight'
            // return 'turnLow';//D
            default:
                return undefined;
        }
    }

    function flyListener(e){
        var flagName = getFlagForKeyCode(e.keyCode);
        if (typeof flagName !=='undefined'){
            flags[flagName] = true;
        }
    }
    
    function removeFlyListener(e){
        var flagName = getFlagForKeyCode(e.keyCode);
        if (typeof flagName !== 'undefined') {
            flags[flagName] = false;
        }
    }
    
    document.onkeydown = flyListener
    document.onkeyup = removeFlyListener

    viewer.clock.onTick.addEventListener(function(clock){
        if(flags.moveForward){
            camera.moveForward(moveRate);
        }
        if(flags.moveBackward){
            camera.moveBackward(moveRate);
        }
        if(flags.moveUp){
            camera.moveUp(moveRate);
        }
        if(flags.moveDown){
            camera.moveDown(moveRate);
        }
        if(flags.moveLeft){
            camera.moveLeft(moveRate);
        }
        if(flags.moveRight){
            camera.moveRight(moveRate);
        }
        if(flags.turnLeft){
            camera.lookLeft(moveRate/6);
        }
        if(flags.turnRight){
            camera.lookRight(moveRate/6);
        }
                
    })

}

function stopFly(){
//方法 返回主界面 
    
    //清除动画效果
    clearInterval(pointTimeId); 
    clearInterval(path_fly);
    clearInterval(path_fly_2);
    clearTimeout(path_fly_3);
    clearInterval(follow_path_fly);
    //清除场景内元素
    viewer.entities.removeAll();
    if(viewer.scene.primitives._primitives[1]){
        viewer.scene.primitives.remove(viewer.scene.primitives._primitives[1])
    };
    viewer.scene.scanEffect.show = false;

    viewer.scene.screenSpaceCameraController.enableRotate = true;
    viewer.scene.screenSpaceCameraController.enableTranslate = true;
    viewer.scene.screenSpaceCameraController.enableZoom = true;
    viewer.scene.screenSpaceCameraController.enableTilt = true;
    viewer.scene.screenSpaceCameraController.enableLook = true;

    viewer.scene.camera.flyTo({
        destination: {
            "x": -2386082.94639815,
            "y": 5386499.581618721,
            "z": 2444456.125364229
          },
        orientation:{heading: 0.3733061006089855,pitch: -0.34544631673647364,roll: 6.283176602864074},
        duration: 3
    });

//动画3s

    //开关选项 
    $('#routefly').addClass('hide');
    $('#pathplanning').addClass('hide');
    $('#stopfly').addClass('hide');
    $('#floorclose').addClass('hide');

    setTimeout(function(){
        $('#startfly').removeClass('hide');
        $('#firestart').removeClass('hide');
        $('#escape').removeClass('hide');
        $('#floorclose').removeClass('hide');   
    },3000);

    document.onkeydown = null;
    document.onkeyup = null;

}